package com.leisai.manager.common.utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.*;

/**
 * 字符串工具类
 * 
 */
public class StringUtil {
	public static Pattern pattern = null;

	/**
	 * 判断字符串是否有效
	 * 
	 * @param str
	 * @return false:str为null或空白字符串
	 */
	public static boolean isValid(String str) {
		if (null == str || "".equals(str.trim())) {
			return false;
		}

		return true;
	}

	/**
	 * 去除开头的0
	 * 
	 * @param str
	 * @return
	 */
	public static String trimStartZero(String str) {
		if (null == str) {
			return str;
		}

		while (str.startsWith("0")) {
			str = str.replaceFirst("0", "");
		}

		return str;

	}

	/**
	 * 空字符串转换成空白字符串
	 * 
	 * @param str
	 *            源字符串
	 * @return
	 */
	public static String null2Empty(String str) {
		return null2Empty(str, false);
	}

	/**
	 * 空字符串转换成空白字符串并去除左右空格
	 * 
	 * @param str
	 *            源字符串
	 * @return
	 */
	public static String null2EmptyTrim(String str) {
		return null2Empty(str, true);
	}

	/**
	 * 空字符串转换成空白字符串
	 * 
	 * @param str
	 *            源字符串
	 * @param trim
	 *            是否去除左右空格
	 * @return
	 */
	public static String null2Empty(String str, boolean trim) {
		if (null == str) {
			return "";
		}

		if (trim) {
			return str.trim();
		} else {
			return str;
		}
	}

	/**
	 * 将字符串格式化为length长度(前补0)
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String formatStr(String str, int length) {
		String returnStr = str;
		for (int i = 0; i < (length - str.length()); i++) {
			returnStr = "0" + returnStr;
		}
		return returnStr;
	}

	/**
	 * 判断string是否是纯数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		if (null != str && !"".equals(str.trim())) {
			pattern = compile("[0-9]*");
			Matcher matcher = pattern.matcher(str);
			return matcher.matches();
		} else {
			return false;
		}
	}

	/**
	 * 校验字符串是否为非负整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isInt(String str) {
		if (null != str && !"".equals(str.trim())) {
			pattern = compile("^\\d+$");
			Matcher matcher = pattern.matcher(str);
			return matcher.matches();
		} else {
			return false;
		}
	}

	/**
	 * 校验字符串是否为正整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isPositiveInt(String str) {
		if (null != str && !"".equals(str.trim())) {
			Pattern pattern = compile("^[0-9]*[1-9][0-9]*$");
			Matcher matcher = pattern.matcher(str);
			return matcher.matches();
		} else {
			return false;
		}
	}

	/**
	 * BigDecimal类型转换为String类型
	 * 
	 * @param dec
	 * @return
	 */
	public static String dec2Str(BigDecimal dec) {
		if (null != dec) {
			return dec.toString();
		} else {
			return "";
		}
	}

	/**
	 * String类型转换为BigDecimal类型
	 * 
	 * @param str
	 * @return
	 */
	public static BigDecimal str2Dec(String str) {
		if (null != str && !"".equals(str.trim())) {
			if(str.endsWith("-")){
				str = "-" + str.substring(0, str.length()-1);
			}
			return new BigDecimal(str.trim());
		} else {
			return null;
		}
	}

	/**
	 * 将BigDecimal类型根据传入小数位数进行四舍五入
	 * 
	 * @param dec
	 * @param num
	 * @return
	 */
	public static String dec2Str(BigDecimal dec, int num) {
		BigDecimal afterDec = dec.setScale(num, BigDecimal.ROUND_HALF_UP);
		return afterDec.toString();
	}

	/**
	 * 校验字符串是否为X位的整数 适用场景：数字位数是固定的
	 * 
	 * @param str
	 *            待确定数字字符串
	 * @param range
	 *            确定位数
	 * @param isInt
	 *            是否包含小数
	 * @return true：匹配，false：不匹配
	 */
	public static boolean isNumber(String str, int range, boolean isInt) {
		boolean result = false;
		int count = 0;
		String regex1 = "^(\\d+)(\\.?+)(0*)$";
		String regex2 = "^\\d+$";
		if (null != str && isInt && str.matches(regex1)) {
			char[] charArray = str.toCharArray();
			for (int i = 0; i < charArray.length; i++) {
				if (!".".equals(String.valueOf(charArray[i]))) {
					count++;
				} else {
					break;
				}
			}
			if (count == range) {
				result = true;
			}
		} else if (null != str && !isInt && str.matches(regex2)) {
			int length = str.length();
			if (length == range) {
				result = true;
			}
		}
		return result;
	}

	/**
	 * 将数字字符串用千分符分隔
	 * 
	 * @param str
	 * @return 含千分符的字符串
	 */
	public static String strAddThsdSept(String str) {
		StringBuffer sb = new StringBuffer();
		String sign = "";
		if (null != str && !"".equals(str.trim())) {
			if (str.startsWith("-") || str.startsWith("+")) {
				sign = str.substring(0, 1);
				str = str.substring(1);
			}
			String[] split = str.split("\\.");
			if (split.length != 0) {
				formatStr(reverseStr(split[0]), sb, 3);
				sb.reverse();
			}
			if (split.length == 2) {
				sb.append(".");
				sb.append(split[1]);
			}
			return sign + sb.toString();
		} else {
			return str;
		}
	}

	/**
	 * 将数字字符串中的千分符去掉
	 * 
	 * @param str
	 * @return
	 */
	public static String strDropThsdSept(String str) {
		StringBuffer sb = new StringBuffer();
		if (null != str && !"".equals(str.trim())) {
			for (int i = 0; i < str.length(); i++) {
				Character charAt = str.charAt(i);
				if (!",".equals(charAt.toString())) {
					sb.append(charAt);
				}
			}
			return sb.toString();
		} else {
			return str;
		}
	}

	/**
	 * 校验字符串是否为大于0小于等于1之间的字符串
	 * 
	 * @param str
	 * @return
	 * @throws Exception
	 *             如果传入的字符串不是数字,会抛出异常
	 */
	public static boolean isBetweenZeroAndOne(String str) throws Exception {
		if (null != str && !"".equals(str.trim())) {
			BigDecimal dec = str2Dec(str);
			BigDecimal decZero = new BigDecimal(0);
			BigDecimal decOne = new BigDecimal(1);
			if (dec.compareTo(decZero) == 1 && dec.compareTo(decOne) <= 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 去掉字符串前面的0 例如：000001203133，返回：1203133
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceStartWithZero(String str) {
		if (null != str && !"".equals(str.trim())) {
			Long valueOf = Long.valueOf(str);
			return valueOf.toString();
		} else {
			return "";
		}
	}

	/**
	 * 填补字符串前面0，例如：08201290，返回0008201290
	 * 
	 * @param str
	 *            需要填补的字符串
	 * @param length
	 *            填补的位数
	 * @return
	 */
	public static String repairStartWithZero(String str, int length) {
		if (null != str && !"".equals(str.trim())) {
			if (str.length() > 0 && str.length() < length) {
				while (length > str.length()) {
					str = "0" + str;
				}
			} else {
				str = "";
			}

			return str;
		} else {
			return "";
		}
	}

	/**
	 * 将字符串转换为大写
	 * 
	 * @param str
	 * @return
	 */
	public static String upperStr(String str) {
		if (null != str && !"".equals(str.trim())) {
			String upperStr = str.toUpperCase();
			return upperStr;
		} else {
			return "";
		}
	}

	/**
	 * 将字符串转换为小写
	 * 
	 * @param str
	 * @return
	 */
	public static String lowerStr(String str) {
		if (null != str && !"".equals(str.trim())) {
			String lowerStr = str.toLowerCase();
			return lowerStr;
		} else {
			return "";
		}
	}

	/**
	 * 添加千分符
	 * 
	 * @param str
	 * @param sb
	 * @param num
	 */
	private static void formatStr(String str, StringBuffer sb, int num) {
		for (int i = 0; i < str.length(); i++) {
			if (i != 0 && i % num == 0) {
				sb.append(",");
			}
			sb.append(str.charAt(i));
		}
	}

	/**
	 * 将字符串倒序
	 * 
	 * @param str
	 * @return
	 */
	private static String reverseStr(String str) {
		StringBuffer sb = new StringBuffer();
		for (int i = str.length() - 1; i >= 0; i--) {
			sb.append(str.charAt(i));
		}
		return sb.toString();
	}

	/**
	 * 数字转为中文一二三四
	 * 
	 * @param numstr
	 * @return
	 */
	public static String num2Chinese(String numstr) {

		String units[] = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };

		String nums[] = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };

		String result[];

		String out = "";

		result = new String[numstr.length()];
		for (int i = 0; i < result.length; i++) {
			result[i] = String.valueOf(numstr.charAt(i));
		}

		int back = 0;

		for (int i = 0; i < result.length; i++) {
			if (!result[i].equals("0")) {
				back = result.length - i - 1;
				out += nums[Integer.parseInt(result[i])];
				out += units[back];
			} else {
				if (i == result.length - 1) {

				} else {
					if (!result[i + 1].equals("0")) {
						out += nums[0];
					}
				}
			}
		}

		return out;
	}

	/**
	 * sql In字符串拼接
	 * 
	 * @param strs
	 * @return
	 */
	public static String sqlIn(List<String> strs) {

		StringBuffer sb = new StringBuffer();

		for (String str : strs) {
			sb.append("'" + str + "',");
		}

		return sb.substring(0, sb.length() - 1);
	}



	/**
	 * 判断是否为中文
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str) {
		String regEx = "[\u4e00-\u9fa5]";
		Pattern pat = compile(regEx);
		Matcher matcher = pat.matcher(str);
		boolean flg = false;
		if (matcher.find()) {
			flg = true;
		}
		return flg;
	}
	

	
	/**
	 * 检查输入字符串得长度(str.length)
	 * @param inputStr
	 * @param minLength
	 * @param maxLength
	 * @return
	 * Err,输入字符串为空
	 * Err,输入字符串的长度小于最小长度
	 * Err,输入字符串的长度大于最大长度
	 */
	public static String checStrLength(String inputStr,int minLength,int maxLength){
		String result = null;
		if(null == inputStr){
			return "Err,输入字符串为空";
		}
		int length = inputStr.length();
		if(minLength<=length && maxLength >= length){
			result = "OK,"+length;
		}else if(minLength > length){
			result = "Err,输入字符串的长度小于最小长度(当前输入长度为："+length+"字符，当前业务最小输入最小长度为："+minLength+"字符)";
		}else{
			result = "Err,输入字符串的长度大于最大长度(当前输入长度为："+length+"字符，当前业务最大输入最大长度为："+maxLength+"字符)";
		}
		return result;
	}
	
	/**
	 * 去掉输入字符串的最后一个字符
	 * @param inputStr
	 * @return
	 */
	public static String cutEndChar(String inputStr){
		String result = "";
		if(StringUtil.isValid(inputStr)){
			result = inputStr.substring(0,inputStr.length() - 1);
		}
		return result;
	}
	/**
	 * 将字符串首字母转大写返回
	 * @param str
	 * @return
	 */
	public static  String  iUpperCase(String str){
		 char[] cs=str.toCharArray();
		 if (Character.isLowerCase(cs[0]) ) {
			 cs[0]-=32;
		}
		 return String.valueOf(cs);
	}
	public static String nullToZero(String str) {
		if ( null == str || "".equals(str)) {
			str = "0";
		}
		return str;
	}

	/**
	 * 去除中括号 []
	 * @param str
	 * @return
	 */
	public  static  String getBrace(String str){
		String regEx = "(\\[[^\\]]*\\])";
		Pattern pat = compile(regEx);
		Matcher matcher = pat.matcher(str);
		boolean flg = false;
		if (matcher.find()) {
			str = matcher.group().substring(1, matcher.group().length()-1);
		}
		return str;
	}
}
